<?php
function sys(){
    /*
    $q = mysql_query('select pokaz,id,title from catalog where pokaz<>""');
    while ($r=mysql_fetch_assoc($q)){
        $array=explode(';',$r['pokaz']);
        $data=array();
        foreach ($array as $value) {
            $d2=explode(',',$value);
            foreach ($d2 as $value) {
                $data= array_merge($data,explode('.',$value));
            }
        }
        foreach ($data as $v){
            $v=trim($v);
            if (!empty($v)){
                $pok_name=hash('md5', $v);
                $pok= mysql_fetch_assoc(mysql_query('select id from pokaz where hash="'.$pok_name.'"'));
                if (isset($pok['id'])){
                    $idpok=$pok['id'];
                    echo $v.'-ready';
                }else{
                    mysql_query('insert into pokaz(title,hash)values("'.$v.'", "'.$pok_name.'")');
                    $idpok=  mysql_insert_id();
                    echo $v.'-new';
                }
                $alias=  mysql_fetch_assoc(mysql_query('select id from cat_pokaz where idcat='.$r['id'].' and idpokaz='.$idpok));
                if (!isset($alias['id'])){
                    echo ' alias <br/>';
                    mysql_query('insert into cat_pokaz(idcat, idpokaz)values('.$r['id'].', '.$idpok.')');
                }else{
                    echo ' ready<br/>';
                }
            }            
        }
    }
     */
    /*
     * Парсим состав
     */
    /*
    $q = mysql_query('select sostav,id,title from catalog where sostav<>""');
    while ($r=mysql_fetch_assoc($q)){
        $array=explode(';',$r['sostav']);
        $data=array();
        foreach ($array as $value) {
            $d2=explode(',',$value);
            foreach ($d2 as $value) {
                $data= array_merge($data,explode('.',$value));
            }
        }
        foreach ($data as $v){
            $v=trim($v);
            if (!empty($v)){
                $pok_name=hash('md5', $v);
                $pok= mysql_fetch_assoc(mysql_query('select id from sostav where hash="'.$pok_name.'"'));
                if (isset($pok['id'])){
                    $idpok=$pok['id'];
                    echo $v.'-ready';
                }else{
                    mysql_query('insert into sostav(title,hash)values("'.$v.'", "'.$pok_name.'")');
                    $idpok=  mysql_insert_id();
                    echo $v.'-new';
                }
                $alias=  mysql_fetch_assoc(mysql_query('select id from cat_sostav where idcat='.$r['id'].' and idsost='.$idpok));
                if (!isset($alias['id'])){
                    echo ' alias <br/>';
                    mysql_query('insert into cat_sostav(idcat, idsost)values('.$r['id'].', '.$idpok.')');
                }else{
                    echo ' ready<br/>';
                }
            }            
        }
    }
    */
    /*
    $q=  mysql_query('select * from catalog where pokaz="" limit 0,10');
    while ($r=  mysql_fetch_assoc($q)){
        echo file_get_contents('http://slovari.yandex.ru/'.urlencode($r['title']).'/%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5/');
    }
     * 
     */
    /*
    $q = mysql_query('select id, zid from catalog where target="lek"');
    while ($r=mysql_fetch_assoc($q)){
        $ch=  mysql_fetch_assoc(mysql_query('select id from cat_zab where idcat='.$r['id'].' and idzab='.$r['zid']));
        if (!isset($ch['id'])){
            mysql_query('insert into cat_zab(idcat,idzab) values('.$r['id'].','.$r['zid'].')');
        }
    }    
     * 
     */
    /*
    $q = mysql_query('select id, forma from catalog where target="lek"');
    while ($r=mysql_fetch_assoc($q)){
            $v=trim($r['forma']);
            if (!empty($v)){
                $pok_name=hash('md5', strtolower($v)); 
                $pok= mysql_fetch_assoc(mysql_query('select id from forma where hash="'.$pok_name.'"'));
                if (isset($pok['id'])){
                    $idpok=$pok['id'];
                    echo $v.'-ready';
                }else{
                    mysql_query('insert into forma(name,hash)values("'.$v.'", "'.$pok_name.'")');
                    $idpok=  mysql_insert_id();
                    echo strtolower($v).'-new';
                }
                mysql_query('update catalog set idforma='.$idpok.' where id='.$r['id']);
            }            
    }    
    */
    /*
    $q = mysql_query('select id, title from cos_series ');
    while ($r=mysql_fetch_assoc($q)){
    		$name = translit(trim(stripslashes($r['title'])));
		// вместо пробела - дефис
		$name = ereg_replace(' ','-',$name);
		 $name = substr(preg_replace('/[^a-z0-9_-]/i','',$name),0,25);
                 mysql_query('update cos_series set name="'.$name.'" where id='.$r['id']);
    }
    exit();
     * 
     */
    function sys_nam($name){
    		$name = translit(trim(stripslashes($name)));
		// вместо пробела - дефис
		$name = ereg_replace(' ','-',$name);
		$name = substr(preg_replace('/[^a-z0-9_-]/i','',$name),0,25);
                return $name;
        
    }
    function hash_($hash){
        $hash= str_ireplace(',', '', $hash);
        $hash= str_ireplace(' ', '', $hash);
        $hash= str_ireplace('  ', '', $hash);
        $hash= str_ireplace('»', '', $hash);
        $hash= str_ireplace('«', '', $hash);
        $hash= str_ireplace('&nbsp;', '', $hash);
        
        return md5(preg_replace("/[^a-zа-я\s,0-9]/ui","",$hash));
    }
    $cat= mysql_query('select * from catalog where target="cos"');
    while ($r=  mysql_fetch_assoc($cat)){        
        $d=hash_($r['title']);
        mysql_query('update catalog set title_hash="'.$d.'" where id='.$r['id']);
    }
    
    $q = mysql_query('select * from apteka_catalog');
    $c=0;
    while ($r=mysql_fetch_assoc($q)){
        $md=hash_($r['name']);
        echo $md.'<br/>';
        $cat= mysql_fetch_assoc(mysql_query('select * from catalog where title_hash="'.$md.'" and target="cos"'));
        if (!empty($cat['id'])){
                $tempFile = '/home/u22700/gomeopat66.ru/www/temp_auto/images/'.$r['pid'].'.jpg';
                if (file_exists($tempFile)){
                    echo $tempFile.'<br/>';
                    $targetPath = '/home/u22700/gomeopat66.ru/www/images/catalog/';

                    $targetFile =  str_replace('//','/',$targetPath.'/'.$cat['id']).'.jpg';
                    try {
                        copy($tempFile,$targetFile);
                        file_put_contents('12', $targetFile);
                        $dst=str_ireplace('.jpg', '---235.jpg', $targetFile);
                        _easyResize($targetFile, $dst,90,230);
                        $dst=str_ireplace('.jpg', '---100.jpg', $targetFile);
                        _easyResize($targetFile, $dst,90,100);
                    }catch (Exception $e) {
                        file_put_contents('123', $e->getMessage());
                    }
                }
                $r['descr']=mysql_real_escape_string($r['descr']);
                /*
                $firm=  mysql_fetch_assoc(mysql_query('select * from firms where title="'.$r['parent'].'"'));
                $firmid=0;
                
                if (empty($firm['id'])){                    
                    mysql_query('insert into firms (name,title)values("'.sys_nam($r['parent']).'","'.$r['parent'].'")') or die(mysql_error());
                    $firmid=  mysql_insert_id();
                }else{
                    $firmid=$firm['id'];
                }
                $firm=  mysql_fetch_assoc(mysql_query('select * from cos_series where title="'.$r['line'].'"'));
                $serid=0;
                if (empty($firm['id'])){                    
                    mysql_query('insert into cos_series (name,title, is_from_auto)values("'.sys_nam($r['line']).'","'.$r['line'].'",1)') or die(mysql_error());
                    $serid=  mysql_insert_id();
                }else{
                    $serid=$firm['id'];
                }
                mysql_query('update catalog set description="'.$r['descr'].'", fid='.$firmid.', sid='.$serid.' where id='.$cat['id']) or die(mysql_error().'update catalog set description="'.$r['descr'].'" where id='.$cat['id']);
                */
                mysql_query('update catalog set description="'.$r['descr'].'" where id='.$cat['id']) or die(mysql_error().'update catalog set description="'.$r['descr'].'" where id='.$cat['id']);
            $c++;
        
    }else{
        echo '<br/>'.'select * from catalog where title_hash="'.$md.'" and target="cos"'.'<br/>'.$r['name'].'<br/>';
    }
    }
    echo 'finded '.$c;
    exit();
}
?>
